#ifndef DRAW_CIRCLE_DELEGATE_H
#define DRAW_CIRCLE_DELEGATE_H

#include <QStyledItemDelegate>
#include <QPainter>
#include <QStyleOptionViewItem>
#include <QModelIndex>
#include <QRect>

class draw_circle_delegate: public QStyledItemDelegate {
public:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
        QStyleOptionViewItem opt = option;
        initStyleOption(&opt, index);

        painter->save();

        // 获取单元格矩形
        QRect rect = option.rect;

        // 计算圆形的矩形
        int diameter = qMin(rect.width(), rect.height());
        QRect circleRect(rect.topLeft(), QSize(diameter, diameter)*0.5);
        circleRect.moveCenter(rect.center());

        // 绘制圆形
        painter->setRenderHint(QPainter::Antialiasing);
        painter->setBrush(QBrush(color_displayed));  // 设置圆形颜色
        painter->drawEllipse(circleRect);

        painter->restore();
    }

    void paint_ChangeColor(const QColor &color){
        color_displayed = color;  // 设置圆形颜色
    }

private:
    QColor color_displayed;
};

#endif // DRAW_CIRCLE_DELEGATE_H
